package com.briup.smartcity_group7.service.impl;

import com.briup.smartcity_group7.bean.basic.Article;
import com.briup.smartcity_group7.bean.ext.ArticleExt;
import com.briup.smartcity_group7.dao.basic.ArticleMapper;
import com.briup.smartcity_group7.dao.basic.BaseUserMapper;
import com.briup.smartcity_group7.dao.basic.CategoryMapper;
import com.briup.smartcity_group7.dao.ext.ArticleExtMapper;
import com.briup.smartcity_group7.service.IArticleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ArticleServiceImpl implements IArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private BaseUserMapper baseUserMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ArticleExtMapper articleExtMapper;

    @Override
    public ArticleExt findByIdWithCategoryAndUser(int id) {
        Article article = articleMapper.selectByPrimaryKey(id);
        ArticleExt articleExt = new ArticleExt();
        BeanUtils.copyProperties(article,articleExt);
        articleExt.setBaseUser(baseUserMapper.selectByPrimaryKey(article.getArticleUserId()));
        articleExt.setCategory(categoryMapper.selectByPrimaryKey(article.getArticleCategoryId()));
        return articleExt;
    }

    @Override
    public void saveOrUpdate(Article article) {
        Article byTitle = articleExtMapper.findByTitle(article.getArticleTitle());
        if(byTitle != null)articleMapper.updateByPrimaryKeySelective(article);
        else articleMapper.insert(article);
    }

    @Override
    public PageInfo<ArticleExt> pageQueryArticleWithCategoryAndUser(int pageNum, int pageSize, String title, Integer status, Integer categoryId) {
        List<Article> articleList = articleExtMapper.findSelective(title, status, categoryId);
        List<ArticleExt> articleExtList = new ArrayList<>();
        for(Article article:articleList){
            ArticleExt articleExt = new ArticleExt();
            BeanUtils.copyProperties(article,articleExt);
            articleExt.setBaseUser(baseUserMapper.selectByPrimaryKey(article.getArticleUserId()));
            articleExt.setCategory(categoryMapper.selectByPrimaryKey(article.getArticleCategoryId()));
            articleExtList.add(articleExt);
        }
        PageInfo<ArticleExt> pageInfo = new PageInfo<>(articleExtList);
        return pageInfo;
    }

    @Override
    public void deleteById(int id) {
        articleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<ArticleExt> pageQueryRecommend(int pageNum, int pageSize) {
        List<Article> articleList = articleExtMapper.findByStatus();
        List<ArticleExt> articleExtList = new ArrayList<>();
        for(Article article:articleList){
            ArticleExt articleExt = new ArticleExt();
            BeanUtils.copyProperties(article,articleExt);
            articleExt.setBaseUser(baseUserMapper.selectByPrimaryKey(article.getArticleUserId()));
            articleExt.setCategory(categoryMapper.selectByPrimaryKey(article.getArticleCategoryId()));
            articleExtList.add(articleExt);
        }
        PageInfo<ArticleExt> pageInfo = new PageInfo<>(articleExtList);
        return pageInfo;
    }

    @Override
    public void changeStatus(int id, int status) {
        articleExtMapper.updateStatusById(id,status);
    }
}
